package com.sjy.project.fym.api;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.AesEncrypt;
import com.sjy.common.utils.DateUtils;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.TokenUtil;
import com.sjy.common.utils.WeChatUtils;
import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.framework.web.controller.BaseController;
import com.sjy.project.fym.accessToken.domain.FyAccessToken;
import com.sjy.project.fym.accessToken.service.IFyAccessTokenService;
import com.sjy.project.fym.user.domain.FyUser;
import com.sjy.project.fym.user.service.IFyUserService;
import com.sjy.project.system.user.domain.User;
import com.sjy.project.system.user.service.IUserService;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(value = "经开区防疫用户信息", description = "设计院对接登录相关接口", tags = {"经开区防疫-登录"})
@RequestMapping({"/api/fym/login"})
public class ApiLoginController extends BaseController {
  @Autowired
  private IUserService userService;
  @Autowired
  private IFyAccessTokenService fyAccessTokenService;
  
  @Autowired
  private TokenUtil tokenUtil;

  @ApiOperation(value = "100.获取token", position = 1,notes = "俩参数统一经过aes base64加密，key:wz_common_key_!!  偏移量:wz_common_vi_!!!"
  		+ "加密类型：AES/CBC/PKCS5Padding 加密模式:AES 编码方式:UTF-8")
	@SuppressWarnings("unchecked")
	@ApiImplicitParams({
       @ApiImplicitParam(required=true,name="key",value="aes加密后秘钥",example="yyyy-mm-dd"),
       @ApiImplicitParam(required=false,name="userId",value="aes加密后用户编号",example="123"),
       @ApiImplicitParam(required=false,name="loginName",value="aes加密后用户工号",example="123")
    })
	@RequestMapping(value = "/noToken/getToken",method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<BaseResponse<Object>> getToken(@RequestParam(required = true) String key,String userId,String loginName,HttpServletRequest request) {
		try {
			String realKey = AesEncrypt.decryptWithBase64(key);
			String realUserId = AesEncrypt.decryptWithBase64(userId);
			String realLoginName = AesEncrypt.decryptWithBase64(loginName);
			if(StringUtils.isBlank(realKey)||(StringUtils.isBlank(realUserId)&&StringUtils.isBlank(realLoginName))) {
				return BaseResponse.generateBadResponseEntity("参数异常", null);
			}
			if(!DateUtils.getDate().equals(realKey)) {
				return BaseResponse.generateBadResponseEntity("key无效", key);
			}
			User user = null;
			if(StringUtils.isNotBlank(userId)) {
				user = userService.selectUserById(Long.parseLong(realUserId));
			}
			if(StringUtils.isNotBlank(loginName)) {
				user = userService.selectUserByLoginName(realLoginName);
			}
			
			if(user==null||"1".equals(user.getStatus())) {
				return BaseResponse.generateBadResponseEntity("账号不存在或已停用", user);
			}
			String token = this.tokenUtil.generateToken(user.getPhonenumber(), "00",user.getUserId(), null,user.getUserName(),user.getLoginName());        
			return BaseResponse.generateOKResponseEntity(token);
		}catch (Exception e) {
			return BaseResponse.generateBadResponseEntity("加密数据格式错误 解密异常!", e.getMessage());
		}
	}
}
